﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using static UtilPublic.Redis.ServerRedis;
using static ServerBase.DataBaseRedis.RdsTxt;
using System.Collections.Generic;
using static UtilPublic.Serialization.Jsh;
using UtilPublic.ServerLoger;
using System;
using UtilPublic.Serialization;

namespace ServerPublic.Data
{
    /// <summary>
    /// 数据方法
    /// </summary>
    public static class PubDataMethodMgr
    {
        

        #region 对象Kv存取
        public static void ObjkvSave<T>(string dataname, T obj)
        {
            ZKvSet(dataname, Jsh.Jstr(obj));
        }
        public static bool ObjKvLoad<T>(string dataname, out T obj) where T : new()
        {
            var data = ZKvGet(dataname);
            return J2O(data, out obj);
        }
        #endregion

        #region 对象hash存取
        public static void ObjHashSave<T>(string dataname, object uid, T obj)
        {
            ZHashSet(dataname, uid, Jsh.Jstr(obj));
        }
        public static bool ObjHashLoad<T>(string dataname, object uid, out T obj) where T : new()
        {
            var data = ZHashGet(dataname, uid);
            return Jsh.J2O(data, out obj);
        }
        public static void ObjHashRemove(string dataname, object uid)
        {
            ZHashDelete(dataname, uid);
        }
        #endregion

        #region 基本数据值
        public static void LoadData(this Dictionary<string, string> dict, string key, ref string value)
        {
            if (dict.TryGetValue(key, out var result)) { value = result; }
        }
        public static void LoadData(this Dictionary<string, string> dict, string key, ref int value)
        {
            if (dict.TryGetValue(key, out var result)) { value = result.ToInt(); }
        }
        public static void LoadData(this Dictionary<string, string> dict, string key, ref long value)
        {
            if (dict.TryGetValue(key, out var result)) { value = result.ToLong(); }
        }
        public static void LoadData(this Dictionary<string, string> dict, string key, ref float value)
        {
            if (dict.TryGetValue(key, out var result)) { value = result.ToFloat(); }
        }
        public static void LoadData(this Dictionary<string, string> dict, string key, ref bool value)
        {
            if (dict.TryGetValue(key, out var result)) { value = result.ToBool(); }
        }
        public static void LoadData(this Dictionary<string, string> dict, string key, ref DateTime value)
        {
            if (dict.TryGetValue(key, out var result)) { value = result.ToDateTime(); }
        }
        public static void LoadDataMod<T>(this Dictionary<string, string> dict, string key, ref T value)
        {
            if (dict.TryGetValue(key, out var result)) { try { value = Jsh.Jobj<T>(result); } catch { } }
        }
        #endregion

        #region 玩家数据模块
        public static bool LoadPlayerMod<T>(long Puid, ref T data, string dataname)
        {
            try
            {
                var datatext = AHashGet($"{RdsPlayerData}:{Puid}", dataname);
                if (string.IsNullOrEmpty(datatext))
                {
                    return false;
                }
                else
                {
                    data = Jsh.Jobj<T>(datatext);
                }
            }
            catch
            {
                loger.Fatal($"读取档案错误 {dataname} Puid:{Puid} dataname{dataname}");
                return false;
            }
            return true;
        }
        // 读取玩家数据模块
        public static bool LoadPlayerModExt<T>(long Puid, out T data, string dataname)
        {
            try
            {
                var datatext = AHashGet($"{RdsPlayerData}:{Puid}", dataname);
                if (string.IsNullOrEmpty(datatext))
                {
                    data = default(T);
                    return false;
                }
                else
                {
                    data = Jsh.Jobj<T>(datatext);
                }
            }
            catch
            {
                data = default(T);
                loger.Fatal($"读取档案错误 {dataname} Puid:{Puid} dataname{dataname}");
                return false;
            }
            return true;
        }
        // 存储玩家数据模块
        public static void SavePlayerMod<T>(long Puid, T data, string dataname)
        {
            AHashSet($"{RdsPlayerData}:{Puid}", dataname, Jsh.Jstr(data));
        }
        #endregion

    }
}